package com.woniuxy.creditsystem.systemmag.controller;

import com.github.pagehelper.PageInfo;

import com.woniuxy.creditsystem.common.bean.DownloadInfo;
import com.woniuxy.creditsystem.common.bean.RoleInfo;
import com.woniuxy.creditsystem.common.bean.TableDataBean;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.util.ResultMsg;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.systemmag.service.IUserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 用户的增、删、查、改
 * @author hejie
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private IUserService userServiceImpl;
    //从application.properties中获取poi的excel文件存放路径
    @Value("${poiFilePath}")
    private String poiFilePath;

    /**新增一个user用户
     * @author hejie
     * UserBean
     * @param
     * @return
     */
    @PostMapping(value="/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveAdmin(Long roleInfo_id, UserInfo userInfo){
        System.out.println("\nroleInfo_id="+roleInfo_id+"\tuserInfo="+userInfo+"\t"+TraceUtil.positionTrace());


        RoleInfo roleInfo=new RoleInfo();
        roleInfo.setId(roleInfo_id);
        userInfo.setRoleInfo(roleInfo);
        System.out.println("\nuserBean="+ userInfo +"\t"+ TraceUtil.positionTrace());

        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);

        //盐值 加密(第三个参数是盐值，为了防止同样的用户名 加密后的密码一样：不同的盐值加密相同的密码，加密后的密文就不一样了)
        String pwd= new SimpleHash("MD5", userInfo.getPassword(), userInfo.getUserName(),1024).toString();
        //这样：数据库中的密码就是密文了
        userInfo.setPassword(pwd);
        userServiceImpl.saveUserBean(userInfo);
        return resultMsg;
    }

    /**
     * 把前台传回来的table中的UserInfo list集合 导出到一个Excel文件
     * @author hejie
     * @param tableDataBean= tableDataJson(前端表格数据)+ simpleFileNameUserSet（用户定义的表格名字）
     * @return ResultMsg
     */
    @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public ResultMsg tableDataJson(@RequestBody TableDataBean<UserInfo> tableDataBean) throws IOException {
        List<UserInfo> tableDataJson=tableDataBean.getTableDataJson();
        String simpleFileNameUserSet=tableDataBean.getSimpleFileNameUserSet();

        userServiceImpl.write2excel(tableDataJson,poiFilePath, simpleFileNameUserSet);

        System.out.println("\n"+TraceUtil.positionTrace());
        for (UserInfo userInfo:tableDataJson) {
            System.out.println(userInfo);
        }
        ResultMsg resultMsg = new ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }

    /**
     * 修改用户
     * @author hejie
     * @param userInfo
     * @param id
     * @return
     */
    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateAdmin(UserInfo userInfo, @PathVariable("id") Long id,String rawPassword){
        System.out.println("\n"+"\tuserInfo="+userInfo+"\t"+TraceUtil.positionTrace());
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        userServiceImpl.updateUserBean(userInfo);
        return resultMsg;
    }

    /**
     * 登录用户 修改自己的密码
     * @param password
     * @return
     */
    @PutMapping(value="/updateMyPassword",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateMyPassword(String  password){
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        userServiceImpl.updateMyPassword(password);
        return resultMsg;
    }

    /**
     * 分页查询数据
     * @author hejie
     * @param userInfo 查询数据
     * @return
     */
    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public PageInfo<UserInfo> findAllByParams(int pageNum,Long roleId, UserInfo userInfo){
        System.out.println("\nuserInfo"+userInfo+"\tpageNum="+pageNum+"\t"+TraceUtil.positionTrace());
        if (userInfo!=null && roleId != null) {
            RoleInfo roleInfo=new RoleInfo();
            roleInfo.setId(roleId);
            userInfo.setRoleInfo(roleInfo);
        }
        int pageNum2 = 2;//这个我测试之后是：0和1都是一样
        int pageSize = 10;
        PageInfo<UserInfo> page = userServiceImpl.findAllByParams(pageNum,pageSize, userInfo);
        System.out.println("page.getPageNum()="+page.getPageNum());
        System.out.println("page.getPageSize()="+page.getPageSize());
        System.out.println("page.getTotal()="+page.getTotal());
//        List<UserInfo> pageDatas = page.getList();
        System.out.println("page.getList()="+page.getList());
        return page;
    }

    /**
     * 删除一些 用户
     * @author hejie
     * @param ids
     * @return
     */
    @RequestMapping(value="/{ids}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(@PathVariable("ids") Long[] ids){
        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);

        for (Long id:ids) {
            userServiceImpl.deleteUserBean(id);
        }
        return resultMsg;
    }
}
